#!/usr/bin/env Rscript

library(tidyverse)

brasil <- read_csv2("../app/data/brasil.csv")
data <- read_csv2("../app/data/dados_cat.csv", na = "NA",
                     col_types = cols(
                         st_acidente_feriado = col_character(),
                         ds_agente_causador = col_character(),
                         ano_cat = col_integer(),
                         ds_cnae_classe_cat = col_character(),
                         dt_acidente = col_date(format = "%d/%m/%Y"),
                         st_dia_semana_acidente = col_character(),
                         ds_emitente_cat = col_character(),
                         hora_acidente = col_time(format = "%H%M"),
                         idade_cat = col_integer(),
                         cd_indica_obito = col_character(),
                         nm_municipio = col_character(),
                         nome_uf = col_character(),
                         ds_natureza_lesao = col_character(),
                         ds_cbo = col_character(),
                         ds_parte_corpo_atingida = col_character(),
                         cd_tipo_sexo_empregado_cat = col_character(),
                         ds_tipo_acidente = col_character(),
                         ds_tipo_local_acidente = col_character()
                     ))

# Use better variable names for dataset and put locality data in front
data <- rename(data, uf = nome_uf,
                  municipio = nm_municipio) %>%
    select(uf, municipio, everything())

# Add correponding locality data from brasil to data
complete <- brasil %>% inner_join(data, by = c("uf", "municipio"))
write_delim(complete, "../app/data/completo.csv", delim = ";")

# Number of accidents:
country <- group_by(complete, pais) %>% summarize(acidentes = n())
by_region <- group_by(complete, regiao) %>% summarize(acidentes = n())
by_uf <- group_by(complete, uf) %>% summarize(acidentes = n())
by_meso <- group_by(complete, mesorregiao) %>% summarize(acidentes = n())
by_micro <- group_by(complete, microrregiao) %>% summarize(acidentes = n())
by_town <- group_by(complete, uf, municipio) %>% summarize(acidentes = n())

# Put every accident alongside locality (this is temporary)
acidentes <- brasil %>% inner_join(country, by = c("pais")) %>%
    inner_join(by_region, by = c("regiao")) %>%
    inner_join(by_uf, by = c("uf")) %>%
    inner_join(by_meso, by = c("mesorregiao")) %>%
    inner_join(by_micro, by = c("microrregiao")) %>%
    inner_join(by_town, by = c("uf", "municipio")) %>%
    select(pais,
           total = acidentes.x,
           regiao,
           acidentes_regiao = acidentes.y,
           uf,
           acidentes_uf = acidentes.x.x,
           mesorregiao,
           acidentes_meso = acidentes.y.y,
           microrregiao,
           acidentes_micro = acidentes.x.x.x,
           municipio,
           acidentes_municipio = acidentes.y.y.y)

# Repete last two columns so we can inspect a single town on parallel sets
acidentes <- acidentes %>%
    mutate(final = municipio, acidentes_final = acidentes_municipio)

write_delim(acidentes, "../app/data/acidentes.csv", delim = ";")
